<template>
    <div class="bg">
        <div class="bg-bottom">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23862.svg"
                alt="">
        </div>
        <div class="bg-bottom">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23863.svg"
                alt="">
        </div>
        <div class="bg-bottom">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23864.svg"
                alt="">
        </div>
        <div class="circle-first">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23865.svg"
                alt="">
        </div>
        <div class="circle-second">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23865.svg"
                alt="">
        </div>
        <div class="circle-thirdly">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23867.svg"
                alt="">
        </div>
        <div class="circle-fourthly">
            <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/u23870.svg"
                alt="">
        </div>
        <div class="login">
            <div class="left-login">
                <img src="https://cdn7.axureshop.com/demo2024/2178128/images/%E7%89%A9%E4%B8%9A%E4%BF%A1%E6%81%AF%E5%B9%B3%E5%8F%B0%E7%99%BB%E5%BD%95/__2323_u23909.png"
                    alt="">
            </div>
            <div class="right-login">
                <h2>XXXXX智慧社区/小区</h2>
                <p>物业信息管理平台</p>
                <el-tabs v-model="activeName" @tab-click="handleClick" class="ipt-box">
                    <el-tab-pane label="账号登录" name="first">
                        <div class="ipt">
                            <el-input placeholder="请输入账号" v-model="userIpt" clearable></el-input>
                        </div>
                        <div class="ipt">
                            <el-input placeholder="请输入登录密码" v-model="pwdIpt" show-password></el-input>
                        </div>
                        <div class="code">
                            <div class="code-left">
                                <el-input v-model="codeIpt" :disabled="userIpt == '' || pwdIpt == ''"
                                    placeholder="请输入验证码"></el-input>
                            </div>
                            <div class="code-right" @click="generateCaptcha">
                                <canvas id="captcha-canvas" width="150" height="42"></canvas>
                            </div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="验证码登录" name="second">
                        <div class="ipt">
                            <el-input placeholder="请输入手机号" v-model="phoneIpt" clearable></el-input>
                        </div>
                        <div class="ipt code-ipt">
                            <el-input placeholder="请输入验证码" v-model="phoneCode" clearable></el-input>
                            <div class="getCode">
                                <span>获取验证码</span>
                            </div>
                        </div>
                    </el-tab-pane>
                </el-tabs>
                <div class="agreement">
                    <input type="checkbox" :checked="isSelect" @change="select">
                    <span>已阅读智慧小区平台</span>
                    <span>《隐私权政策》</span>
                </div>
                <div class="login-btn" @click="login">登录</div>
                <div class="forgetPwd">
                    忘记密码?
                </div>
            </div>
        </div>

    </div>
</template>

<script>
export default {
    data() {
        return {
            activeName: 'first',
            userIpt: '',
            pwdIpt: '',
            codeIpt: '',
            phoneIpt: '',
            phoneCode: '',
            //复选框
            isSelect: false,
            //随机验证码
            randomCode: '',
        };
    },
    methods: {
        reset() {
            this.userIpt = '',
                this.pwdIpt = '',
                this.codeIpt = '',
                this.generateCaptcha()
        },
        select() {
            this.isSelect = !this.isSelect
        },
        //点击登录按钮
        login() {
            if (this.userIpt == '' || this.pwdIpt == '') {
                this.$message({
                    message: '账号或密码不能为空',
                    type: 'warning',
                    center: true
                });
                this.reset()
                return
            }
            if (this.codeIpt == '') {
                this.$message({
                    message: '验证码为空,请重新输入',
                    type: 'warning',
                    center: true
                });
                this.generateCaptcha()
                return
            }
            if (this.codeIpt.includes(this.randomCode)) {
                this.$message({
                    message: '验证码输入有误,请重新输入',
                    type: 'warning',
                    center: true
                });
                this.generateCaptcha()
                this.codeIpt = ''
                return
            }
            if (!this.isSelect) {
                this.$message({
                    message: '请阅读用户隐私政策',
                    type: 'warning',
                    center: true
                });
                return
            }
            if (this.userIpt == 'zhangsan' && this.pwdIpt == 123456) {
                this.$message({
                    message: '登录成功',
                    type: 'success',
                    center: true
                });
            } else {
                this.$message({
                    message: '账号或密码错误,请重新输入',
                    type: 'error',
                    center: true
                });
                this.reset()

            }
        },
        handleClick(tab, event) {
            console.log(tab, event);
        },
        getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        },
        generateCaptcha() {
            const canvas = document.getElementById('captcha-canvas');
            const ctx = canvas.getContext('2d');

            // Clear the canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // Define characters and captcha length
            const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            const captchaLength = 4;
            let captcha = '';

            // Generate random captcha text and colors
            for (let i = 0; i < captchaLength; i++) {
                const randomIndex = Math.floor(Math.random() * characters.length);
                captcha += characters[randomIndex];
            }
            this.randomCode = captcha

            // Draw captcha text on canvas with random colors
            ctx.font = '24px Arial';
            ctx.textBaseline = 'middle';

            for (let i = 0; i < captcha.length; i++) {
                ctx.save();

                // Random position for each character (simple centering)
                const x = (i + 1) * (canvas.width / (captcha.length + 1)) - 12; // Adjust -12 to center better
                const y = canvas.height / 2;

                // Random rotation
                const angle = Math.random() * 15 - 7.5;
                ctx.translate(x, y);
                ctx.rotate(angle * Math.PI / 180);
                // Random color
                ctx.fillStyle = this.getRandomColor();

                // Draw the character
                ctx.fillText(captcha[i], 0, 0);

                ctx.restore();
            }
        }
    },
    mounted() {
        this.generateCaptcha()
    }
};
</script>

<style scoped>
.bg {
    position: relative;
    height: 100vh;
    background: linear-gradient(-54.940573033113deg, rgba(19, 202, 251, 1) 0%, rgba(24, 140, 249, 1) 31%, rgba(26, 118, 248, 1) 51%, rgba(51, 133, 249, 1) 100%);
}

.bg-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 188px;
}

.bg-bottom img {
    width: 100%;
    height: 100%;
}

.circle-first {
    width: 110px;
    height: 110px;
    position: absolute;
    left: 48px;
    top: 87px;
}

.circle-second {
    position: absolute;
    left: 777px;
    top: 314px;
    width: 41px;
    height: 41px;
}

.circle-thirdly {
    position: absolute;
    left: 720px;
    top: 69px;
    width: 35px;
    height: 35px;
}

.circle-fourthly {
    position: absolute;
    right: 84px;
    top: 44px;
    width: 96px;
    height: 96px
}

.ipt {
    width: 350px;
}

.code-left {
    width: 200px;
}

.code {
    display: flex;
    align-items: center;
    margin-top: 5px;
}

.ipt-box>>>.el-tabs__content {
    margin-top: 20px;
}

.code-right {
    background: #fff;
    height: 42px;
    cursor: pointer;
}

input {
    outline: none;
}

.ipt>>>input {
    border-top: 0;
    border-left: 0;
    border-right: 0;
    /* border: 0; */
    border-radius: 0;
    margin-top: 5px;
    height: 42px;
}

.code>>>input {
    border-top: 0;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    height: 42px;
}

.code-ipt {
    width: 248px;
}

.ipt>>>.el-input__icon {
    line-height: 52px;
}

.getCode {
    border: 1px solid rgba(153, 153, 153, 1);
    width: 102px;
    height: 35px;
    line-height: 35px;
    text-align: center;
    border-radius: 50px;
    color: #1E1E1E;
    font-size: 12px;
}

.code-ipt {
    display: flex;
    align-items: center;
    /* background: #fff; */
    justify-content: space-between;
    width: 350px;
}

.left-login img {
    width: 466px;
    height: 452px;
}

.login {
    display: flex;
    width: 886px;
    height: 453px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 20px rgba(51, 102, 255, 0.427450980392157);
    background-color: rgba(255, 255, 255, 1);
    border-radius: 5px;

}

.right-login h2 {
    font-size: 26px;
}

.right-login p {
    font-size: 18px;
    color: #949494;
    margin-bottom: 30px;
}

.right-login {
    background: #fff;
    padding-left: 30px;
    padding-top: 15px;
}

.left-login {
    background: linear-gradient(180deg, rgba(229, 251, 255, 1) 0%, rgba(33, 142, 244, 1) 100%);
    border-radius: 5px;
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
}

.agreement input {
    width: 12px;
    height: 12px;
    margin-right: 5px;
}

.agreement span {
    font-size: 12px;
    color: #409EFF;
}

.agreement {
    display: flex;
    align-items: center;
    margin-top: 40px;
    cursor: pointer;
}

.login-btn {
    width: 350px;
    height: 39px;
    background-color: rgba(0, 121, 254, 0.898039215686275);
    font-size: 16px;
    color: #FFFFFF;
    text-align: center;
    line-height: 39px;
    border-radius: 5px;
    margin-top: 20px;
    cursor: pointer;
}

.forgetPwd {
    font-size: 12px;
    color: #1A87FE;
    padding: 2px 2px 2px 2px;
    cursor: pointer;
}
</style>